<html xmlns="http://www.w3.org/1999/xhtml"><head><style type="text/css">
dt {
  font-style: italic;
  margin-top: 15px;
  margin-bottom: 3px;
  margin-left: 0px;
  border-bottom: 1px dotted black;
}
dd {
  margin-left: 10px;
}
table {
  border-collapse:collapse;
  border: 1px solid black;
  margin-top: 7px;
}
th {
  border: 1px solid black;
  padding: 3 7 3 7;
}
td {
  border: 1px solid black;
  padding: 3 7 3 7;
}
</style></head><body><p><a name="instanceof"></a><b>instanceof</b> :
  test class of object : <a href="opcodes.html">index</a> : <a href="http://asm.objectweb.org/asm40/javadoc/user/org/objectweb/asm/MethodVisitor.html#visitTypeInsn(int,%20java.lang.String)">visitTypeInsn()</a></p><dl><dt>Description</dt><dd>The instanceof instruction is used to implement the Java
language's instanceof operator, which tests whether an object
reference or array belongs to a given class.<br></br>instanceof takes a single parameter, &lt;type&gt;.
&lt;type&gt; is either the name of a Java class, or it is the type
descriptor of an array. <br></br>At runtime, &lt;type&gt; is resolved (Chapter 7 describes how classes are
resolved). Next, instanceof pops <i xmlns="">objectref</i> (a reference to an
object) off the top of the operand stack. If <i xmlns="">objectref</i> is an instance of
&lt;type&gt; or one of &lt;type&gt;'s subclasses, the int 1
is pushed onto the stack, otherwise the int 0 is pushed onto the stack. If
<i xmlns="">objectref</i> is null, the result is always 0. If
&lt;type&gt; is an interface, int-result will be 1 if <i xmlns="">objectref</i>
implements that interface, and 0 if <i xmlns="">objectref</i> does not implement the
interface.</dd><dt>Example</dt><dd><pre>
; using instanceof to test for a String:
    aload_1        ; push object reference in local variable 1 onto stack
    instanceof java/lang/String;      ; test if item on stack is a String
    ifne HaveString                   ; if so, goto HaveString
    return                            ; otherwise, return
HaveString:
    ; if this point is reached, local variable 1 holds a string
; this example uses instanceof to test if local variable 1 holds
; an integer array
    aload_1                  ; push local variable 1 onto the stack
    instanceof [I            ; test if the top item on the stack is an integer array
    ifne HaveIntegerArray    ; if so, jump to HaveIntegerArray
    return                   ; simply return if local variable 1 is not an int array
HaveIntegerArray:            
    ; if this point is reached, local variable 1 holds an integer array
; you can also use instanceof to test that objects implement a given interface,
; e.g.
    aload_1                  ; push local variable 1 onto the stack
    instanceof java/lang/Runnable  ; test if it implements the Runnable interface
    ifne HaveARunnable       ; if so, jump to HaveARunnable
    return                   ; otherwise return
HaveARunnable:            
    ; if this point is reached, local variable 1 holds a reference to an object
    ; that implements the Runnable interface.
</pre></dd><dt>See also</dt><dd><a href="ref-checkcast.html">checkcast</a> </dd><dt>Stack</dt><dd><table xmlns="">
<tr>
<td>
<b>Before </b>
</td>
<td><b>After</b></td></tr>
<tr>
<td>objectref</td>
<td>int-result</td></tr>
<tr>
<td>...</td>
<td>...</td></tr>
</table></dd><dt>Bytecode</dt><dd><table xmlns="">
<tr>
<td>
<b> Type </b>
</td>
<td><b>Description</b></td></tr>
<tr>
<td>
u1 
</td>
<td>instanceof
opcode = 0xC1 (193)
</td></tr>
<tr>
<td>
u2 
</td>
<td>index</td></tr>
</table></dd></dl></body></html>